<template>
    <div class="container">
        <el-form :model="ruleForm2" status-icon :rules="rules2" ref="ruleForm2" label-width="120px"
                 class="demo-ruleForm">
            <el-form-item label="角色名" prop="p1">
                <el-input type="text" v-model="ruleForm2.p1" auto-complete="off"></el-input>
            </el-form-item>
            <!--<el-form-item label="允许登录后台" prop="p1">-->
                <!--<el-radio v-model="ruleForm2.p2" label="0">是</el-radio>-->
                <!--<el-radio v-model="ruleForm2.p2" label="1">否</el-radio>-->
            <!--</el-form-item>-->
            <!--<el-form-item label="允许登录APP" prop="p1">-->
                <!--<el-radio v-model="ruleForm2.p3" label="0">是</el-radio>-->
                <!--<el-radio v-model="ruleForm2.p3" label="1">否</el-radio>-->
            <!--</el-form-item>-->
            <el-form-item label="描述" prop="p4">
                <el-input type="text" v-model="ruleForm2.p4" auto-complete="off"></el-input>
            </el-form-item>

            <el-form-item label="权限管理">
                <el-tree
                    :data="tree"
                    show-checkbox
                    default-expand-all
                    node-key="id"
                    ref="tree"
                    highlight-current
                    :props="defaultProps">
                </el-tree>
            </el-form-item>
            <div style="border-bottom: 1px solid #eee;margin: 0.5rem 0 1rem;"></div>
<!--            <el-form-item label="APP端权限">-->
<!--                <el-tree-->
<!--                    :data="tree2"-->
<!--                    show-checkbox-->
<!--                    default-expand-all-->
<!--                    node-key="id"-->
<!--                    ref="tree2"-->
<!--                    highlight-current-->
<!--                    :props="defaultProps">-->
<!--                </el-tree>-->
<!--            </el-form-item>-->
            <br>
            <el-form-item>
                <el-button type="primary" @click="submitForm('ruleForm2')">提交</el-button>
                <!--<el-button @click="getCheckedNodes">获取</el-button>-->
            </el-form-item>
        </el-form>
    </div>
</template>

<script>
    import {quillEditor} from 'vue-quill-editor';
    import pub from '../../utils/pub'

    export default {
        data: function () {
            var v1 = (rule, value, callback) => {
                if (value === '') {
                    callback(new Error('请输入角色名称'));
                } else if (value < 2 || value.length > 10) {
                    callback(new Error('2-10位'));
                } else {
                    callback();
                }
            };
            return {
                tree: [],
                tree2: [],
                defaultProps: {
                    children: 'children',
                    label: 'name'
                },
                arr: [],
                arr2: [],
                ruleForm2: {
                    p1: '',
                    p2: '0',
                    p3: '',
                    p4: '',
                },
                rules2: {
                    p1: [
                        {required: true, validator: v1, trigger: 'blur'}
                    ],
                    p2: [
                        {required: true, trigger: 'blur'}
                    ],
                    p3: [
                        {required: true, trigger: 'blur'}
                    ],
                }
            }
        },
        components: {
            quillEditor
        },
        props: ['uid'],
        methods: {
            loadopen() {
                this.loading = this.$loading({
                    lock: true,
                    text: 'Loading',
                    spinner: 'el-icon-loading',
                    background: 'rgba(0, 0, 0, 0.2)'
                });
                setTimeout(() => {this.loading.close();}, 5000);
            },
            loadclose() {
                this.loading.close();
            },
            // 获取角色信息
            getInfo() {
                this.setCheckedKeys([]);
                // this.setCheckedKeys2([]);
                if(!this.uid) {
                    this.ruleForm2.p1 = '';
                    this.ruleForm2.p2 = '0';
                    this.ruleForm2.p3 = '0';
                    this.ruleForm2.p4 = '';
                    return;
                }
                this.loadopen();
                this.config.get('/admin/manager/role/findById', {id: this.uid}).then(res => {
                    this.loadclose();
                    if (res.data.code === '0') {
                        // console.log(res.data, '获取角色信息');
                        this.ruleForm2.p1 = res.data.role.name;
                        // this.ruleForm2.p2 = res.data.role.is_login_manage;
                        // this.ruleForm2.p3 = res.data.role.is_login_app;
                        this.ruleForm2.p4 = res.data.role.role_remark;
                        this.setCheckedKeys(res.data.ids.split(',') || []);
                        // this.setCheckedKeys2(res.data.appMenuIds.split(',') || []);
                    } else {
                        this.$message.error(this.config.handleError(res.data));
                    }
                });
            },
            // 获取所有权限菜单
            getMenu(){
                this.config.post('/admin/manager/role/getMenuList', {}).then(res=>{
                    if(res.data.code==='0'){
                        let arr = res.data.menuList;
                        this.arr = res.data.menuList;
                        this.tree = pub.arrayToTreeList(arr, 'id', 'pid');
                    }else{
                        this.$message.error(this.config.handleError(res.data));
                    }
                })
            },
            // // 获取APP所有权限菜单
            // getAppMenu(){
            //     this.config.post('/admin/manager/role/getAppMenuList', {}).then(res=>{
            //         if(res.data.code==='0'){
            //             let arr2 = res.data.menuAppList;
            //             this.arr2 = res.data.menuAppList;
            //             this.tree2 = pub.arrayToTreeList(arr2, 'id', 'pid');
            //         }else{
            //             this.$message.error(this.config.handleError(res.data));
            //         }
            //     })
            // },
            // 获取选择的菜单ids
            getCheckedNodes() {
                // console.log(this.$refs.tree.getCheckedNodes());
                let arr = [];
                this.$refs.tree.getCheckedNodes().forEach(item=>{
                    arr.push(item.id);
                    if(arr.indexOf(item.pid)==-1){
                        arr.push(item.pid);
                    }
                });
                return arr;
            },
            setCheckedKeys(ids) {
                let arr = [];
                this.arr.forEach(it=>{
                    if(it.children&&it.children.length){
                        if(ids.indexOf(it.id)>=0){
                            ids.splice(ids.indexOf(it.id), 1);
                        }
                    }
                });
                this.$refs.tree.setCheckedKeys(ids);
            },
            // 获取选择的菜单ids
            // getCheckedNodes2() {
            //     // console.log(this.$refs.tree.getCheckedNodes());
            //     let arr = [];
            //     this.$refs.tree2.getCheckedNodes().forEach(item=>{
            //         arr.push(item.id);
            //         if(arr.indexOf(item.pid)==-1){
            //             arr.push(item.pid);
            //         }
            //     });
            //     return arr;
            // },
            // setCheckedKeys2(ids) {
            //     let arr = [];
            //     this.arr2.forEach(it=>{
            //         if(it.children&&it.children.length){
            //             if(ids.indexOf(it.id)>=0){
            //                 ids.splice(ids.indexOf(it.id), 1);
            //             }
            //         }
            //     });
            //     this.$refs.tree2.setCheckedKeys(ids);
            // },
            submitForm(formName) {
                this.$refs[formName].validate((valid) => {
                    if (valid) {
                        Array.prototype.remove = function(val) {
                            let index = this.indexOf(val);
                            if (index > -1) {
                                this.splice(index, 1);
                            }
                        };
                        let ids = this.getCheckedNodes();
                        // let appIds = this.getCheckedNodes2();
                        ids.remove('0');
                        // appIds.remove('0');
                        let obj = {
                            'role.id': this.uid,
                            'role.name': this.ruleForm2.p1,
                            'role.role_remark': this.ruleForm2.p4,
                            // 'role.is_login_manage': this.ruleForm2.p2,
                            // 'role.is_login_app': this.ruleForm2.p3,
                            'ids': ids.join(','),
                            // 'appIds': appIds.join(','),
                        };
                        //console.log(obj);
                        let url = this.uid ? '/admin/manager/role/update' : '/admin/manager/role/add';
                        this.loadopen();
                        this.config.post(url, obj).then(res => {
                            this.loadclose();
                            if (res.data.code === '0') {
                                this.$message({
                                    message: res.data.message,
                                    type: 'success'
                                });
                            } else {
                                this.$message.error(this.config.handleError(res.data));
                            }
                            this.$emit('done', '');
                        });
                    } else {
                        console.log('error submit!!');
                        return false;
                    }
                });
            },
        },
        mounted() {
            this.getMenu();
            // this.getAppMenu();
        }
    }
</script>
